﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Profile;

namespace DNA.Mvc.Community
{
    public class PostWrapper : Post
    {
        private Post innerPost;
        private UserProfile profile;

        public PostWrapper(Post post)
        {
            innerPost = post;
        }

        #region Overrides

        public override string DeleteBy
        {
            get
            {
                return innerPost.DeleteBy;
            }
            set
            {
                innerPost.DeleteBy = value;
            }
        }

        public override int? ParentID
        {
            get
            {
                return innerPost.ParentID;
            }
            set
            {
                innerPost.ParentID = value;
            }
        }

        public override DateTime Posted
        {
            get
            {
                return innerPost.Posted;
            }
            set
            {
                innerPost.Posted = value;
            }
        }

        public override int Reads
        {
            get
            {
                return innerPost.Reads;
            }
            set
            {
                innerPost.Reads = value;
            }
        }

        public override string UserName
        {
            get
            {
                return innerPost.UserName;
            }
            set
            {
                innerPost.UserName = value;
            }
        }

        public override string Title
        {
            get
            {
                return innerPost.Title;
            }
            set
            {
                innerPost.Title = value;
            }
        }

        public override int ThreadID
        {
            get
            {
                return innerPost.ThreadID;
            }
            set
            {
                innerPost.ThreadID = value;
            }
        }

        public override Thread Thread
        {
            get
            {
                return innerPost.Thread;
            }
            set
            {
                innerPost.Thread = value;
            }
        }

        public override int Ratings
        {
            get
            {
                return innerPost.Ratings;
            }
            set
            {
                innerPost.Ratings = value;
            }
        }

        public override int Pos
        {
            get
            {
                return innerPost.Pos;
            }
            set
            {
                innerPost.Pos = value;
            }
        }

        public override string Moderator
        {
            get
            {
                return innerPost.Moderator;
            }
            set
            {
                innerPost.Moderator = value;
            }
        }

        public override System.DateTime LastModified
        {
            get
            {
                return innerPost.LastModified;
            }
            set
            {
                innerPost.LastModified = value;
            }
        }

        public override bool IsThread
        {
            get
            {
                return innerPost.IsThread;
            }
            set
            {
                innerPost.IsThread = value;
            }
        }

        public override bool IsLocked
        {
            get
            {
                return innerPost.IsLocked;
            }
            set
            {
                innerPost.IsLocked = value;
            }
        }

        public override bool IsDeleted
        {
            get
            {
                return innerPost.IsDeleted;
            }
            set
            {
                innerPost.IsDeleted = value;
            }
        }

        public override bool IsApproved
        {
            get
            {
                return innerPost.IsApproved;
            }
            set
            {
                innerPost.IsApproved = value;
            }
        }

        public override bool IsAnswer
        {
            get
            {
                return innerPost.IsAnswer;
            }
            set
            {
                innerPost.IsAnswer = value;
            }
        }

        public override string IP
        {
            get
            {
                return innerPost.IP;
            }
            set
            {
                innerPost.IP = value;
            }
        }

        public override string DeletedReason
        {
            get
            {
                return innerPost.DeletedReason;
            }
            set
            {
                innerPost.DeletedReason = value;
            }
        }

        public override DateTime Deleted
        {
            get
            {
                return innerPost.Deleted;
            }
            set
            {
                if (innerPost != null)
                    innerPost.Deleted = value;
            }
        }

        public override string Body
        {
            get
            {
                return innerPost.Body;
            }
            set
            {
                innerPost.Body = value;
            }
        }

        public override ICollection<Attachment> Attachments
        {
            get
            {
                return innerPost.Attachments;
            }
            set
            {
                innerPost.Attachments = value;
            }
        }

        public override DateTime Approved
        {
            get
            {
                return innerPost.Approved;
            }
            set
            {
                if (innerPost != null)
                    innerPost.Approved = value;
            }
        }

        public override int ID
        {
            get
            {
                return innerPost.ID;
            }
            set
            {
                innerPost.ID = value;
            }
        }

        #endregion

        public bool IsOwner(HttpContextBase httpContext)
        {
            if (httpContext.Request.IsAuthenticated)
                return httpContext.User.Identity.Name.Equals(this.UserName);
            return false;
        }

        public UserProfile Profile
        {
            get
            {
                if (this.profile == null)
                    profile = UserProfile.GetProfile(UserName);
                return profile;
            }
        }

        //[Obsolete]
        //public ProfileBase UserProfile
        //{
        //    get
        //    {
        //        if (!string.IsNullOrEmpty(this.UserName))
        //        {
        //            ProfileBase profile = new ProfileBase();
        //            profile.Initialize(this.UserName, true);
        //            return profile;
        //        }
        //        return null;
        //    }
        //}

        public bool IsModerator(HttpContextBase httpContext)
        {
            if (httpContext.Request.IsAuthenticated)
            {
                if (HttpContext.Current.User.IsInRole("administrators"))
                    return true;
                return Thread.Forum.Moderators.FirstOrDefault(m => m.UserName.Equals(httpContext.User.Identity.Name)) != null;
            }
            return false;
        }

        public string GetPermalink(HttpContextBase httpContext)
        {
            if (this.ThreadID > 0)
            {
                if (!IsThread || Pos > 20)
                {
                    var _posts = this.Thread.Posts.ToList();
                    var count = _posts.Count();
                    var seqIndex = _posts.IndexOf(innerPost);
                    double _intPart = Math.Truncate((double)seqIndex / (double)20);
                    var _mod = (seqIndex % 20) > 0 ? 1 : 0;
                    var index = Convert.ToInt32(_intPart) + _mod;
                    string linkFormat = UrlResolver.Resolve(httpContext) + "/community/thread/index/" + ThreadID.ToString() + "?index=" + index + "&size=20#" + ID.ToString();
                }

                return UrlResolver.Resolve(httpContext) + "/community/thread/index/" + ThreadID.ToString()+"#"+this.ID.ToString();
            }
            return "";
        }
    }
}
